import Vue from 'vue'
import Vuex from 'vuex'

import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'

const state={
  token:'',
  user:'',
}

const actions={
 saveToken(context,playload){
    return new Promise((resolve,reject)=>{
      context.commit('SAVE_TOKEN', playload)
      resolve('已记录token')
    })
  },
  saveUser(context,playload){
    return new Promise((resolve,reject)=>{
      context.commit('SAVE_USER', playload)
      resolve('已记录user')
    })
  }

}
const mutations={
  SAVE_TOKEN(state,playload){
    state.token=playload;
  },
  SAVE_USER(state,playload){
    state.user=playload;
  },
}
Vue.use(Vuex)

export default new Vuex.Store({
  plugins:[createPersistedState({
    storage: {
      getItem: key => Cookies.get(key),
      setItem: (key, value) => Cookies.set(key, value, { expires: 7 }),
      removeItem: key => Cookies.remove(key)
    },
    key:'store',
    render(state){
      return{...state};
    }
  })],
  state,
  mutations,
  actions,
})
